package com.water.wfo.component.startup;

import ch.qos.logback.classic.Level;
import com.water.wfo.component.constant.Constants;
import com.water.wfo.component.constant.SystemConstants;
import com.water.wfo.component.constant.enums.EnvEnum;
import com.water.wfo.component.util.BootUtil;
import com.water.wfo.component.util.StringUtil;
import com.water.wfo.component.util.SystemPropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

@Slf4j
public abstract class AbstractRunApp {
    /**
     *  application startup
     * @param clazz
     * @param args
     */
    protected static void startup(Class<? extends AbstractRunApp> clazz, String[] args)  {

        // springBootConfig
        String configPath = BootUtil.getBootConfigPath();

        // 系统配置文件
        String bootConfigFile = configPath + SystemConstants.BOOT_CONFIG_FILES;

        // 读取system-config.properties属性
        Properties bootConfigProperties = readBootConfig(bootConfigFile);

        SystemPropertiesUtil.setSystemProperties(bootConfigProperties);
        // 日志配置
        logConfig(bootConfigProperties);

        new SpringApplicationBuilder(clazz)
                .properties(SystemConstants.SPRING_CONFIG_PATH + "=" + bootConfigFile)
                .web(WebApplicationType.SERVLET)
                .run(args);
    }

    /**
     * 读取system-config.properties
     *
     * @param bootConfigFile
     * @return
     */
    protected static Properties readBootConfig(String bootConfigFile){
        Properties props = new Properties();

        try(InputStream input = new FileInputStream(bootConfigFile);
            Reader reader = new InputStreamReader(input, StandardCharsets.UTF_8)){
            props.load(reader);
        }catch (IOException e) {
            log.error("system-config.properties读取失败",e);
        }
        return props;
    }

    /**
     * 日志配置
     *
     * @param bootConfigProperties
     */
    protected static void logConfig(Properties bootConfigProperties){

        // 设置环境
        String env = bootConfigProperties.getProperty(SystemConstants.APPLICATION_ENV, EnvEnum.PROD.name());
        System.setProperty(SystemConstants.APPLICATION_ENV, env);

        // springboot配置的应用日志级别，默认INFO
        String logLevel = bootConfigProperties.getProperty(SystemConstants.APPLICATION_LOG_LEVEL, SystemConstants.APPLICATION_LOG_LEVEL_DEFAULT);
        System.setProperty(SystemConstants.APPLICATION_LOG_LEVEL, logLevel);

        // 是否打印console日志
        String logConsole = bootConfigProperties.getProperty(SystemConstants.APPLICATION_LOG_CONSOLE, Constants.SUCCESS_STRING);
        System.setProperty(SystemConstants.APPLICATION_LOG_CONSOLE, logConsole);

        // 日志级别为DEBUG，则开启打印sql
        if(Level.DEBUG.equals(Level.valueOf(logLevel.toUpperCase()))) {
            String logClass = SystemConstants.ORG_APACHE_IBATIS_LOGGING_STDOUT_STD_OUT_IMPL;
            System.setProperty(SystemConstants.MYBATIS_CONFIGURATION_LOG_IMPL, logClass);
        }

        // 获取应用名称，设置日志文件路径
        String applicationName = bootConfigProperties.getProperty(SystemConstants.SPRING_APPLICATION_NAME);
        if(StringUtil.isBlank(applicationName)) {
            applicationName = bootConfigProperties.getProperty(SystemConstants.NSF_APPLICATION_NAME);
        }
        // 设置日志级别
        System.setProperty(SystemConstants.SPRING_APPLICATION_NAME, applicationName);

        // 配置日志xml文件路径
        System.setProperty(SystemConstants.LOGGING_CONFIG, SystemConstants.LOGBACK_SPRING_XML);

    }

}
